#include "AdvancedWindow.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QGroupBox>
#include <QFormLayout>

AdvancedWindow::AdvancedWindow(QWidget* parent) : ElaWidget(parent)
{
    setWindowTitle("高级窗口配置示例");
    resize(500, 400);
    //moveToCenter();

    // 初始窗口行为配置
    setIsStayTop(true);         // 置顶显示
    setIsFixedSize(true);       // 固定大小，不可调整
    setIsDefaultClosed(false);  // 不启用默认关闭
    setAppBarHeight(45);        // 设置标题栏高度

    // 启用所有按钮用于演示
    setWindowButtonFlags(ElaAppBarType::StayTopButtonHint | ElaAppBarType::MinimizeButtonHint | ElaAppBarType::MaximizeButtonHint | ElaAppBarType::CloseButtonHint | ElaAppBarType::ThemeChangeButtonHint | ElaAppBarType::NavigationButtonHint);

    setupUI();
}

void AdvancedWindow::setupUI()
{
    QWidget* content = new QWidget(this);
    content->setStyleSheet(
        "background: qlineargradient(x1:0, y1:0, x2:1, y2:1, stop:0 #667eea, stop:1 #764ba2);"
        "border-radius: 10px;");

    // 创建控制面板
    QWidget* controlPanel = new QWidget(this);
    controlPanel->setStyleSheet(
        "background: white;"
        "border-radius: 8px;"
        "padding: 15px;");

    // 创建控件
    m_stayTopCheck = new QCheckBox("窗口置顶", controlPanel);
    m_stayTopCheck->setChecked(getIsStayTop());
    connect(m_stayTopCheck, &QCheckBox::toggled, this, &AdvancedWindow::onStayTopToggled);

    m_fixedSizeCheck = new QCheckBox("固定窗口大小", controlPanel);
    m_fixedSizeCheck->setChecked(getIsFixedSize());
    connect(m_fixedSizeCheck, &QCheckBox::toggled, this, &AdvancedWindow::onFixedSizeToggled);

    m_heightLabel = new QLabel(QString("标题栏高度: %1px").arg(getAppBarHeight()), controlPanel);
    m_heightSlider = new QSlider(Qt::Horizontal, controlPanel);
    m_heightSlider->setRange(30, 80);
    m_heightSlider->setValue(getAppBarHeight());
    m_heightSlider->setTickPosition(QSlider::TicksBelow);
    m_heightSlider->setTickInterval(10);
    connect(m_heightSlider, &QSlider::valueChanged, this, &AdvancedWindow::onAppBarHeightChanged);

    m_progressBar = new QProgressBar(controlPanel);
    m_progressBar->setRange(0, 100);
    m_progressBar->setValue(0);
    m_progressBar->setTextVisible(true);
    connect(m_heightSlider, &QSlider::valueChanged, this, &AdvancedWindow::updateProgressBar);

    // 控制面板布局
    QFormLayout* formLayout = new QFormLayout(controlPanel);
    formLayout->addRow("窗口行为:", m_stayTopCheck);
    formLayout->addRow("", m_fixedSizeCheck);
    formLayout->addRow(m_heightLabel);
    formLayout->addRow(m_heightSlider);
    formLayout->addRow("进度指示:", m_progressBar);

    // 主布局
    QVBoxLayout* mainLayout = new QVBoxLayout(this);
    mainLayout->setContentsMargins(30, getAppBarHeight() + 30, 30, 30);
    mainLayout->setSpacing(20);

    // 渐变背景区域
    QHBoxLayout* gradientLayout = new QHBoxLayout();
    gradientLayout->addStretch();

    QLabel* gradientLabel = new QLabel("渐变背景区域", content);
    gradientLabel->setStyleSheet(
        "color: white;"
        "font-size: 18px;"
        "font-weight: bold;"
        "background: transparent;");
    gradientLabel->setAlignment(Qt::AlignCenter);

    gradientLayout->addWidget(gradientLabel);
    gradientLayout->addStretch();

    content->setLayout(gradientLayout);

    mainLayout->addWidget(content, 2);       // 2/3的空间给渐变区域
    mainLayout->addWidget(controlPanel, 1);  // 1/3的空间给控制面板

    setLayout(mainLayout);
}

void AdvancedWindow::onStayTopToggled(bool checked)
{
    setIsStayTop(checked);
    QString status = checked ? "已启用" : "已禁用";
    m_stayTopCheck->setText(QString("窗口置顶 (%1)").arg(status));
}

void AdvancedWindow::onFixedSizeToggled(bool checked)
{
    setIsFixedSize(checked);
    QString status = checked ? "已启用" : "已禁用";
    m_fixedSizeCheck->setText(QString("固定窗口大小 (%1)").arg(status));
}

void AdvancedWindow::onAppBarHeightChanged(int value)
{
    setAppBarHeight(value);
    m_heightLabel->setText(QString("标题栏高度: %1px").arg(value));

    // 更新布局边距
    if (layout())
    {
        layout()->setContentsMargins(30, value + 30, 30, 30);
    }
}

void AdvancedWindow::updateProgressBar(int value)
{
    m_progressBar->setValue(value);
}